package user_interface;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.rmi.RemoteException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import embeded.EmbededObject;
import embeded.impl.EmbededObjectImpl;

public class Main extends JFrame {
	public JTextField textTopicName = new JTextField(20);
	public JTextArea textData = new JTextArea();
	public JButton buttonUse = new JButton("Use"), buttonSubmit = new JButton(
			"Submit"), buttonEnd = new JButton("End using"),
			buttonExit = new JButton("Exit");

	EmbededObject embededObject = null;
	Thread t;

	public Main() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(350, 200);
		setLayout(new BorderLayout());
		setLocationRelativeTo(null);
		test();
		setVisible(true);
		buttonUse.setEnabled(true);
		buttonSubmit.setEnabled(false);
		buttonEnd.setEnabled(false);
		textTopicName.setEnabled(true);
		textData.setText("");
		textData.setEnabled(false);
	}

	public void test() {
		JPanel northPane = new JPanel(new FlowLayout(FlowLayout.LEFT));
		northPane.add(textTopicName);
		northPane.add(buttonUse);
		getContentPane().add("North", northPane);

		JPanel centerPane = new JPanel(new BorderLayout()), pane1 = new JPanel(
				new FlowLayout(FlowLayout.RIGHT));
		pane1.add(buttonSubmit);

		centerPane.add("Center", textData);
		centerPane.add("South", pane1);
		getContentPane().add("Center", centerPane);

		JPanel southPane = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		southPane.add(buttonEnd);
		southPane.add(buttonExit);
		getContentPane().add("South", southPane);

		buttonUse.addMouseListener(new UseListener(this));
		buttonSubmit.addMouseListener(new SubmitListener(this));
		buttonEnd.addMouseListener(new EndUsingListener(this));
		buttonExit.addMouseListener(new ExitListener());

	}

	public void endUsing() {
		try {
			if (embededObject != null) {
				embededObject.endUsing();
				buttonUse.setEnabled(true);
				buttonSubmit.setEnabled(false);
				buttonEnd.setEnabled(false);
				textTopicName.setEnabled(true);
				textData.setText("");
				textData.setEnabled(false);
			}
		} catch (RemoteException e1) {
			System.out.println("erreur d'utilisation");
			e1.printStackTrace();
		}
	}

	public static void main(String[] args) {
		new Main();
	}

	public class UseListener implements MouseListener {
		Main main;

		public UseListener(Main main) {
			this.main = main;
		}

		public void mouseClicked(MouseEvent e) {
			if (main.textTopicName.getText().equals(""))
				return;
			main.embededObject = new EmbededObjectImpl();

			t = new Thread(new Runnable() {

				public void run() {
					while (true) {
						try {
							if (embededObject.isRefresh()
									&& !embededObject.isBusy()) {
								textData.setText((String) embededObject
										.getData());
								embededObject.setRefresh(false);
							}
						} catch (RemoteException e) {
							e.printStackTrace();
						}
					}
				}
			});
			t.start();

			try {
				if (main.embededObject.beginUsing(main.textTopicName
						.getText())) {
					buttonUse.setEnabled(false);
					buttonSubmit.setEnabled(true);
					buttonEnd.setEnabled(true);
					textTopicName.setEnabled(false);
					textData.setEnabled(true);
					textData.setText((String) main.embededObject.getData());
				} else {

				}
			} catch (RemoteException e1) {
				System.out.println("erreur d'utilisation");
				e1.printStackTrace();
			}
		}

		public void mousePressed(MouseEvent e) {
		}

		public void mouseReleased(MouseEvent e) {
		}

		public void mouseEntered(MouseEvent e) {
		}

		public void mouseExited(MouseEvent e) {
		}

	}

	public class SubmitListener implements MouseListener {
		Main main;

		public SubmitListener(Main main) {
			this.main = main;
		}

		public void mouseClicked(MouseEvent e) {
			try {
				main.embededObject.setData(main.textData.getText());
			} catch (RemoteException e1) {
				System.out.println("erreur d'utilisation");
				e1.printStackTrace();
			}
		}

		public void mousePressed(MouseEvent e) {
		}

		public void mouseReleased(MouseEvent e) {
		}

		public void mouseEntered(MouseEvent e) {
		}

		public void mouseExited(MouseEvent e) {
		}

	}

	public class EndUsingListener implements MouseListener {
		Main main;

		public EndUsingListener(Main main) {
			this.main = main;
		}

		public void mouseClicked(MouseEvent e) {
			endUsing();
		}

		public void mousePressed(MouseEvent e) {
		}

		public void mouseReleased(MouseEvent e) {
		}

		public void mouseEntered(MouseEvent e) {
		}

		public void mouseExited(MouseEvent e) {
		}

	}

	public class ExitListener implements MouseListener {

		public void mouseClicked(MouseEvent e) {
			endUsing();
			System.exit(0);
		}

		public void mousePressed(MouseEvent e) {
		}

		public void mouseReleased(MouseEvent e) {
		}

		public void mouseEntered(MouseEvent e) {
		}

		public void mouseExited(MouseEvent e) {
		}

	}

}
